﻿using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using ThirdPrize.Classes;
using ThirdPrize.Controllers;

//[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace ThirdPrize
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static System.Threading.Timer timer = new System.Threading.Timer(timer_Elapsed, null, 0, 30 * 60 * 1000); // This will raise the event every 30 mins.

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger("Application");


        protected void Application_Start()
        {
            //logging
            log4net.Config.XmlConfigurator.Configure();
            log.Info("Web site Started");

            //system bits
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

        protected void Application_End()
        {
            timer.Dispose();

            log.Info("Web site Stopped");
        }

        static void timer_Elapsed(object sender)
        {
            log.Info("timer_Elapsed Called");

            try
            {
                // Do Your Stuff
                using(RssController r = new RssController())
	            {
                    r.RssRefreshCore(System.AppDomain.CurrentDomain.BaseDirectory);
                    //log.Info("timer_Elapsed finished");
                }
            }
            catch (System.Exception e)
            {
                log.Error("Exception in timer_Elapsed",e);
            }
        }
    }
}